package com.eportal.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.Serializable;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eportal.base.BaseDAO;
import com.eportal.domain.City;
import com.eportal.domain.FuncAgent;
import com.eportal.domain.FuncApply;
import com.eportal.domain.FuncMain;
import com.eportal.domain.Sysopt;
import com.eportal.domain.TAgent;
import com.eportal.service.CityService;
import com.eportal.service.FuncAgentService;
import com.eportal.service.FuncDetailService;
import com.eportal.service.FuncMainService;
import com.eportal.service.TAgentService;
import com.eportal.util.DateUtils;
import com.framework.jqgrid.JqGridPager;

@Service("funcAgentService")
public class FuncAgentServiceImpl  implements  FuncAgentService {

	
	private Logger log = Logger.getLogger(FuncAgentServiceImpl.class);
	@Autowired
	TAgentService agentService;
	
	@Resource(name="dao")
	private BaseDAO dao;
	@Autowired
	CityService cityService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> list(JqGridPager jqGridPager, String agtCode, String funcMainId) {
		String hql="from FuncAgent where 1=1 ";
		if(agtCode!=null&&agtCode.length()>0){
			hql +=" and agtcode='"+agtCode+"'";
		}
		if(funcMainId!=null&&funcMainId.length()>0){
			hql +=" and mainId='"+funcMainId+"'";
		}
		List<FuncAgent> list = dao.query(hql);
		List datalist =new ArrayList<>();
		for(FuncAgent bean:list){
			Map map = new HashMap();
			map.put("LASTAUDITTIME", "-");
			map.put("LASTAUDITUSERNAME", "-");
			TAgent agent = agentService.get(bean.getAgtcode());
			
			FuncMain main = (FuncMain)dao.getById(FuncMain.class,Long.valueOf( bean.getMainId()));
			int count = dao.count("select count(*) from t03_community where agtcode='"+bean.getAgtcode()+"'") ;
			int open = dao.countQuery("select count(*)  from FuncDetail where main_id='"+bean.getMainId()+"' and agtcode='"+bean.getAgtcode()+"' and status='1'");
			List<FuncApply> alist = dao.query("from FuncApply where (status='2' or status='3') and mainId='"+bean.getMainId()+"' and agtcode='"+bean.getAgtcode()+"' order by sid desc",1,1);
			
			try{
				if(alist.size()>0){
					FuncApply lastAudit = alist.get(0);
					Sysopt sysopt = (Sysopt)dao.getById(Sysopt.class, Long.valueOf(lastAudit.getAuditUsername()));
					map.put("LASTAUDITTIME",DateUtils.formatDatetime(lastAudit.getAuditTime(), "yyyy-MM-dd"));
					map.put("LASTAUDITUSERNAME", sysopt.getUserName());
				}	
			}catch(Exception e){
				log.error(this,e);
			}
			
			City city = (City) cityService.get("from City where dm='"+agent.getAreaCode()+"'");
			if(city!=null){
				map.put("DESCRIBED",city.getDescribed());	
			}else{
				map.put("DESCRIBED","-");
			}
			map.put("AGTNAME", agent.getAgtName());
			map.put("FUNCNAME", main.getCname());
			map.put("LASTAPPLYTIME", DateUtils.formatDatetime(bean.getLastApplyTime(), "yyyy-MM-dd"));
			map.put("ALL", count);
			map.put("OPEN", open);
			map.put("SID",bean.getSid());
			datalist.add(map);
		}
		Map map = new HashMap();
		return datalist;
	}
	
}
